
package com.kde.crisisdepalabras;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.kde.crisisdepalabras.PlayerAdapter.PlayerHolder;
import com.kde.crisisdepalabras.Model.Player;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;


public class DownloadPhotoTask extends AsyncTask<PlayerHolder, Void, PlayerHolder> 
{

	private PlayerHolder playerHolder;
	private Player player;
    private Bitmap bitmap = null;
	
   
    @Override
    protected PlayerHolder doInBackground(PlayerHolder... params) 
    {
    	PlayerHolder viewHolder = params[0];
	    try
	    {
             URL imageURL = new URL(viewHolder.imageURL);
             bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
			 viewHolder.bitmap = bitmap;
  	    }
	    catch (Exception e) 
	    {
 	         Log.e("error", "Downloading Image Failed");
 	         this.playerHolder.avatarImage = null;
 	    }
 	    
	    return viewHolder;
    }

    @Override
    protected void onPostExecute(PlayerHolder result)
    {
    	 if (result.avatarImage == null) 
    	 {
              result.avatarImage.setImageResource(R.drawable.teacher);
	     } 
    	 else 
    	 {
             result.avatarImage.setImageBitmap(result.bitmap);
	     }
    }
}